package com.mofofa.maozy.info.cotediva

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.components.dialog.FcfaRecheckWalletDialog
import com.mofofa.maozy.components.dialog.FcfaDeleteWalletDialog
import com.mofofa.maozy.components.dialog.FcfaNoWalletWarningDialog
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.data.ReqDeleteWalletFcfa
import com.mofofa.maozy.databinding.ActivityMaozyBindWalletFcfaBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent
import com.mofofa.maozy.extend.startMaozyToMaozyNextMaozyActivity
import com.mofofa.maozy.extend.startMaozyToMaozyNextMaozyFromMaozyPage
import com.mofofa.maozy.launch.UserStateViewModel

@SuppressLint("CustomSplashScreen")
class WalletListFcfaActivity : BaseActivity() {

    private lateinit var mMaozyActivityMaozyBinding: ActivityMaozyBindWalletFcfaBinding
    private lateinit var mWalletListVndAdapter: WalletListVndAdapter

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this).get(UserStateViewModel::class.java)
    }

    private val mWalletInfoFcfaViewModel by lazy {
        ViewModelProvider(this).get(WalletInfoFcfaViewModel::class.java)
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoFcfaViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.wallet_fcfa.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mMaozyActivityMaozyBinding =
            ActivityMaozyBindWalletFcfaBinding.inflate(layoutInflater)
        return mMaozyActivityMaozyBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //初始化列表
            mWalletListVndAdapter =
                WalletListVndAdapter(mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA)
            mWalletListVndAdapter.setMaozyOnMaozyItemMaozyClickMaozyListener { index, item, action ->
                if (0 == action) {
                    FcfaDeleteWalletDialog.showMaozyDialog(
                        supportFragmentManager,
                        mutableListOf(item)
                    ) { ids ->
                        if (ids.isNotBlank()) {
                            //删除钱包
                            mWalletInfoFcfaViewModel.deleteMaozyWalletMaozyFCFA(
                                ReqDeleteWalletFcfa(
                                    ids = ids
                                )
                            )
                        }
                    }
                }
            }
            mMaozyActivityMaozyBinding.rcFCFABindWalletList.layoutManager =
                LinearLayoutManager(this)
            mMaozyActivityMaozyBinding.rcFCFABindWalletList.adapter = mWalletListVndAdapter

            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startMaozyToMaozyNextMaozyFromMaozyPage(PageTagEnum.info)
                }
            }

            //获取app配置回调
            mUserStateViewModel.onMaozyFetchMaozySysMaozyConfigMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    //获取钱包列表
                    mWalletInfoFcfaViewModel.getMaozyWalletMaozyListMaozyFCFA(
                        ReqBase(
                            showLoading = false
                        )
                    )
                }
            }

            //获取用户钱包列表回调
            mWalletInfoFcfaViewModel.onMaozyFetchMaozyWalletMaozyListMaozyFCFAMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        result.forEach { item ->
                            item.displayMode = 0
                        }
                        mWalletInfoFcfaViewModel.initMaozyWalletMaozyListMaozyFCFA(
                            result
                        )
                        mWalletListVndAdapter.notifyDataSetChanged()
                    }
                }
            }

            //保存用户钱包回调
            mWalletInfoFcfaViewModel.onMaozySaveMaozyWalletMaozyFCFAMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //获取钱包列表
                        mWalletInfoFcfaViewModel.getMaozyWalletMaozyListMaozyFCFA()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //删除用户钱包列表回调
            mWalletInfoFcfaViewModel.onMaozyDeleteMaozyWalletMaozyFCFAMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //获取钱包列表
                        mWalletInfoFcfaViewModel.getMaozyWalletMaozyListMaozyFCFA()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //保存用户绑卡认证状态回调
            mWalletInfoFcfaViewModel.onMaozySaveMaozyBvnMaozyFCFAMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //查询用户状态
                        mUserStateViewModel.fetchMaozyUserMaozyState()
                    }
                }
            }

            //点击增加新钱包
            mMaozyActivityMaozyBinding.llFCFABindWalletBg.setOnClickListener {
                toMaozyBindMaozyWalletMaozyWaveMaozyPage()
            }
            //点击增加新钱包
            mMaozyActivityMaozyBinding.imageFCFABindWalletAdd.setOnClickListener {
                toMaozyBindMaozyWalletMaozyWaveMaozyPage()
            }
            //点击增加新钱包
            mMaozyActivityMaozyBinding.textFCFABindWalletAdd.setOnClickListener {
                toMaozyBindMaozyWalletMaozyWaveMaozyPage()
            }

            //点击下一步
            mMaozyActivityMaozyBinding.submitFCFABindWallet.setOnClickListener {
                mMaozyActivityMaozyBinding.submitFCFABindWallet.recordMaozyClickMaozyEvent()
                //保存绑卡认证状态
                if (mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA.isNotEmpty()) {
                    mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA.forEach { item ->
                        item.displayMode = 1
                    }
                    FcfaRecheckWalletDialog.showMaozyDialog(
                        supportFragmentManager,
                        mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA
                    ) {
                        mWalletInfoFcfaViewModel.saveMaozyBvnMaozyFCFA(ReqBase())
                    }
                } else {
                    FcfaNoWalletWarningDialog.showMaozyDialog(supportFragmentManager) {}
                }
            }

            //获取钱包列表
            mWalletInfoFcfaViewModel.getMaozyWalletMaozyListMaozyFCFA()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()
        //获取app配置
        mUserStateViewModel.fetchMaozySysMaozyConfig()
    }

    private fun toMaozyBindMaozyWalletMaozyWaveMaozyPage() {
        if (mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA.isEmpty()) {
            //跳转wave列表
            startMaozyToMaozyNextMaozyActivity<BindWalletTypeWaveFcfaActivity>()
        } else {
            //跳转展示列表
            startMaozyToMaozyNextMaozyActivity<WalletListAddResultFcfaActivity>()
        }
    }
}